<template>
  <el-container :v-loading="loading" class="post-article">
    <el-header class="header">
      <el-select
        v-model="comarticle.model"
        placeholder="请选择文章栏目"
        style="width: 150px; margin-top: 10px"
      >
        <el-option
          v-for="item in categories"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        v-model="comarticle.title"
        placeholder="请输入标题..."
        style="width: 400px; margin-left: 5px; margin-top: 10px"
      ></el-input>
    </el-header>
    <el-main class="main">
      <div id="editor">
        <mavon-editor
          style="height: 100%; width: 100%"
          ref=md
          @imgAdd="imgAdd"
          @imgDel="imgDel"
          v-model="comarticle.mdcontent"
        ></mavon-editor>
      </div>
      <div
        style="
          display: flex;
          align-items: center;
          margin-top: 15px;
          justify-content: flex-end;
        "
      >
        <template>
          <el-button type="primary" @click="saveBlog()">发表文章</el-button>
        </template>
        <template>
          <el-button type="primary" @click="giveup()">放弃修改</el-button>
        </template>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { isNotNullORBlank } from "@/utils/utils";
import { articaladd ,uploadFileRequest} from "@/api/apis";
export default {
  name: "Editor",
  data() {
    return {
      loading: true,
      categories: [
        {
          value: "comartic",
          label: "交流合作",
        },
        {
          value: "resultarticle",
          label: "科学研究",
        },
        {
          value: "particleben",
          label: "人才培养-本科生",
        },
        {
          value: "particleyan",
          label: "人才培养-研究生",
        },
      ],
      comarticle: {
        title: "",
        content: "",
        model: "",
        mdcontent: "",
      },
    };
  },
  methods: {
    saveBlog() {
      if (
        !isNotNullORBlank(
          this.comarticle.title,
          this.comarticle.mdcontent,
          // this.comarticle.mdcontent,
          this.comarticle.model
        )
      ) {
        this.$message({ type: "error", message: "数据不能为空!" });
        return;
      }
      // var _this = this;
      this.comarticle.title = this.comarticle.title,
        this.comarticle.content = this.$refs.md.d_render,
        this.comarticle.mdcontent = this.comarticle.mdcontent,
        this.comarticle.model = this.comarticle.model,
        // console.log(this.comarticle)
        articaladd(this.comarticle).then((res) => {
          this.loading = false;
          // console.log(res)
          // console.log(res.data.code)
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "文章发表成功!",
            });
            this.$router.push("/main/artical");
          } else {
            this.$message({
              type: "error",
              message: "请稍后再试!",
            });
          }
        });
    },
    giveup() {
      this.$router.go(-1);
    },
    imgAdd(pos, $file) {
      // console.log(pos)
      console.log($file)
      // console.log(this)
      // 第一步.将图片上传到服务器.
      var formdata = new FormData();
      formdata.append("image", $file);
      uploadFileRequest("/add/uploadimg", formdata).then((res) => {
        console.log(res)
        var json = res.data;
        if (json.code == 200) {
          //            _this.$refs.md.$imgUpdateByUrl(pos, json.msg)
          this.$refs.md.$imglst2Url([[pos, json.data]]);
        } else {
          this.$message({ type: json.code, message: json.data});
        }
      });
    },
    imgDel(pos) {},
  },
};
</script>
<style>
.post-article > .main > #editor {
  width: 100%;
  height: 450px;
  text-align: left;
}

.post-article > .header {
  background-color: #ececec;
  margin-top: 10px;
  padding-left: 5px;
  display: flex;
  justify-content: flex-start;
}

.post-article > .main {
  /*justify-content: flex-start;*/
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  background-color: #ececec;
  padding-top: 0px;
}
</style>

